package com.log.aspect;

import com.log.common.AspectCommon;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author zwj
 * @date 2021-2-23
 */
@Component
@Aspect
@Order(2)
public class PostMappingAspect {

    @Autowired
    private AspectCommon aspectCommon;

    /**
     * 配置切入点
     */
    @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public void PostLogPointcut() {
        // 该方法无方法体,主要为了让同类中其他方法使用此切入点
    }

    /**
     * 配置环绕通知,使用在方法logPointcut()上注册的切入点
     *
     * @param joinPoint join point for advice
     */
    @Around("PostLogPointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result;
        result = joinPoint.proceed();
        aspectCommon.saveINFOLog(joinPoint);
        return result;
    }

    /**
     * 配置异常通知
     *
     * @param joinPoint join point for advice
     * @param e         exception
     */
    @AfterThrowing(pointcut = "PostLogPointcut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable e) {
        aspectCommon.saveErrorLog(joinPoint, e);
    }


}
